package cqut.gbq.qimo.service.impl;

import cqut.gbq.qimo.entity.ResBean;
import cqut.gbq.qimo.mapper.CourseMapper;
import cqut.gbq.qimo.service.CourseService;
import cqut.gbq.qimo.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * @ClassName FileServiceImpl
 * @Description TODO
 * @Author guobenqi
 * @Date 2021/6/18 17:58
 */
@Service
public class FileServiceImpl implements FileService {
    @Autowired
    private CourseMapper courseMapper;

    @Override
    public ResBean uploadFile(MultipartFile file,Integer sid,Integer aid) {


        String fileName = file.getOriginalFilename();
        System.out.println(fileName);
        String path = "G:/ideaWorkSpace2/ketangpai/src/main/resources/static";


        String url = path + "/" + fileName;

        File dest = new File(new File(path).getAbsolutePath()+ "/" + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest); // 保存文件

        } catch (Exception e) {
            e.printStackTrace();
            return ResBean.error("上传作业失败");
        }

       Integer count =  courseMapper.addAssWorkUrl(sid,aid,url);

        if (count > 0){
            return ResBean.success("上传作业成功");

        }else{
            return ResBean.error("上传作业失败");
        }

    }
}
